package com.haotunet.app.goldtreasure.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.MainPagerAdapter;
import com.haotunet.app.goldtreasure.fragment.BuyRecordFragment;
import com.haotunet.app.goldtreasure.fragment.DepositRecordFragment;
import com.haotunet.app.goldtreasure.fragment.ExchangeRecordFragment;
import com.haotunet.app.goldtreasure.fragment.PickRecordFragment;
import com.haotunet.app.goldtreasure.fragment.SellRecordFragment;
import com.haotunet.app.goldtreasure.utils.ActivityUtils;
import com.haotunet.app.goldtreasure.views.TitleView;

import java.util.ArrayList;
import java.util.List;

@Route(path = "/mine/investment_record")
public class TransRecordActivity extends BaseActivity implements OnPageChangeListener {
    @Autowired
    int index;
    private TitleView mTitleView;
    private TextView tvBuy, tvSell, tvDeposit, tvPick, tvExchange;
    private ImageView ivBuyLine, ivSellLine, ivDepositLine, ivpickLine, ivExchangeLine;
    private ViewPager mVpRecord;
    private List<ImageView> ivList;
    private TextView tv_menu[];
    private int prePosition = 0;
    private String type = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trans_record);
        initView();
    }

    private void initView() {
        if (getIntent().hasExtra("type")) {
            type = getIntent().getStringExtra("type");
        }
        if (getIntent().hasExtra("index")) {
            index = getIntent().getIntExtra("index", 0);
        }
        tv_menu = new TextView[5];
        mTitleView = (TitleView) findViewById(R.id.title_view);
        mTitleView.getTitletextView().setVisibility(View.VISIBLE);
        mTitleView.getTitletextView().setText(getString(R.string.transaction_record));
        mTitleView.getBackView().setVisibility(View.VISIBLE);
        mTitleView.getBackView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (type.equals("buyCurrentSuccess") || type.equals("buyRegularSuccess") || type.equals("transSuccess")
                        || type.equals("exchangeSuccess") || type.equals("depositSuccess")
                        || type.equals("pickSuccess")) {
                    skipToMain();
                } else {
                    finish();
                }
            }
        });
        tvBuy = (TextView) findViewById(R.id.tv_buy_gold);
        tvSell = (TextView) findViewById(R.id.tv_sell_gold);
        tvDeposit = (TextView) findViewById(R.id.tv_deposit_gold);
        tvPick = (TextView) findViewById(R.id.tv_pick_gold);
        tvExchange = (TextView) findViewById(R.id.tv_exchange_gold);
        ivBuyLine = (ImageView) findViewById(R.id.iv_buy_line);
        ivSellLine = (ImageView) findViewById(R.id.iv_sell_line);
        ivDepositLine = (ImageView) findViewById(R.id.iv_deposit_line);
        ivpickLine = (ImageView) findViewById(R.id.iv_pick_line);
        ivExchangeLine = (ImageView) findViewById(R.id.iv_exchange_line);
        mVpRecord = (ViewPager) findViewById(R.id.vp_record);
        tv_menu[0] = tvBuy;
        tv_menu[1] = tvSell;
        tv_menu[2] = tvDeposit;
        tv_menu[3] = tvPick;
        tv_menu[4] = tvExchange;
        setListener();
        ivList = new ArrayList<ImageView>();
        ivList.add(ivBuyLine);
        ivList.add(ivSellLine);
        ivList.add(ivDepositLine);
        ivList.add(ivpickLine);
        ivList.add(ivExchangeLine);
        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager());
        adapter.add(BuyRecordFragment.newInstance(type));
        adapter.add(new SellRecordFragment());
        adapter.add(DepositRecordFragment.newInstance(type, index));
        adapter.add(PickRecordFragment.newInstance(type, index));
        adapter.add(ExchangeRecordFragment.newInstance(type, index));
        mVpRecord.setAdapter(adapter);
        mVpRecord.setOffscreenPageLimit(adapter.getCount());
        if (type.equals("exchangeSuccess") || index == 4) {
            onClickIndex(4);
        } else if (type.equals("depositSuccess") || index == 2) {
            onClickIndex(2);
        } else if (type.equals("pickSuccess") || index == 3) {
            onClickIndex(3);
        }
    }

    private void setListener() {

        mVpRecord.addOnPageChangeListener(this);
        for (int i = 0; i < tv_menu.length; i++) {
            final int cur = i;
            tv_menu[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    onClickIndex(cur);
                }

            });
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @SuppressWarnings("deprecation")
    @Override
    public void onPageSelected(int arg0) {
        ivList.get(prePosition).setBackgroundColor(getResources().getColor(R.color.color_white));
        ivList.get(arg0).setBackgroundColor(getResources().getColor(R.color.yellow_btn_bg));
        tv_menu[arg0].setTextColor(getResources().getColor(R.color.txt_yellow));
        tv_menu[prePosition].setTextColor(getResources().getColor(R.color.color_black));
        prePosition = arg0;
    }

    public void onClickIndex(int cur) {
        mVpRecord.setCurrentItem(cur, false);

        for (int i = 0; i < tv_menu.length; i++) {

            if (cur == i) {
                tv_menu[i].setSelected(true);
            } else {
                tv_menu[i].setSelected(false);
            }

        }
    }

    private void skipToMain() {
        ActivityUtils.skipToMain(TransRecordActivity.this, "");
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (type.equals("buyCurrentSuccess") || type.equals("buyRegularSuccess") || type.equals("transSuccess") || type.equals("exchangeSuccess")
                    || type.equals("depositSuccess") || type.equals("pickSuccess")) {
                skipToMain();
            }

        }
        return super.onKeyUp(keyCode, event);
    }
}
